package com.du.spring5.demo;

import com.du.spring5.bean.User;
import com.du.spring5.bean.User1;
import com.du.spring5.bean.User2;
import com.du.spring5.config.SpringConfig;
import com.du.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author : DuJiabao
 * @Date : 2021/2/3 21:44
 * @Project : spring
 * @File : null.java
 * @Desc :
 */
public class Demo {
  @Test
  public void test() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user);
  }

  @Test
  public void test2() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    User user = context.getBean("user", User.class);
    System.out.println(user);

    ApplicationContext context2 = new ClassPathXmlApplicationContext("bean3.xml");
    UserService userService = context.getBean("userService", UserService.class);
    System.out.println(userService);
  }

  @Test
  public void test3() {
    // 通过另一个bean工厂的实现类，通过配置类的方式，导入配置。无需使用xml
    ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); // 和ClassPathXmlApplicationContext是远方表兄弟
    User user = context.getBean("user", User.class);
    System.out.println(user);
  }

  @Test
  public void test4() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
    User1 user1 = context.getBean("user1", User1.class);
    System.out.println(user1);
  }

  @Test
  public void test5() {
    ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    User2 user2 = context.getBean("user2", User2.class);
    System.out.println(user2);
  }

}
